﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using System.Collections.ObjectModel;
using System.Globalization;

namespace WpfAppListView
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {

        List<ResultImage> listResultImage = new List<ResultImage>();


        MyVM4ResultImage myData4ResultImage = null;

        public MainWindow()
        {
            InitializeComponent();


            //数据绑定
            myData4ResultImage = new MyVM4ResultImage();
            myData4ResultImage.PageSize = 30;
            this.DataContext = myData4ResultImage;
            this.listView1.ItemsSource = myData4ResultImage.FakeSource;


          //  this.ContentRendered += MainWindow_ContentRendered;


            this.KeyDown += MainWindow_KeyDown;
        }

        private void MainWindow_KeyDown(object sender, KeyEventArgs e)
        {
           
            
            if (e.Key==Key.S)
            {
                ResultImage sel = listView1.SelectedItem as ResultImage;

                if (sel != null)
                    sel.Show = !sel.Show;
            }
        }

        private void MainWindow_ContentRendered(object sender, EventArgs e)
        {

            string ReviewName = "";

            bool isShow = false;

            for (int i = 1; i <= 10; i++)
            {
                for (int j = 1; j <= 10; j++)
                {
                    if (j % 3 == 0)
                        ReviewName = "OK";
                    else
                        ReviewName = "";

                    if (j <= i)
                        isShow = true;
                    else
                        isShow = false;

                    
                    listResultImage.Add(new ResultImage { CameraNo = "", RowIndex = i, ColIndex = j, ImagePath_NG = "", ReviewName= ReviewName ,Show=isShow});

                    //if (i == j)
                    //    break;

                }
            }

            myData4ResultImage.Source = listResultImage;
        }


        private void Button2_Click(object sender, RoutedEventArgs e)
        {

            listResultImage.Clear();
            string ReviewName = "";

            bool isShow = false;

            int colnum = Convert.ToInt32(txtWidth.Text.Trim());


            listView1.Width = colnum * 66+4;



           double size = listView1.Width / ((double)colnum)-10;


            myData4ResultImage.PageSize = colnum * 4;

            for (int i = 1; i <= 10; i++)
            {
                for (int j = 1; j <= colnum; j++)
                {
                    if (j % 3 == 0)
                        ReviewName = "OK";
                    else
                        ReviewName = "";

                    if (j <= i)
                        isShow = true;
                    else
                        isShow = false;

                    isShow = true;

                    listResultImage.Add(new ResultImage { Width=size, Height=size, CameraNo = "", RowIndex = i, ColIndex = j, ImagePath_NG = "", ReviewName = ReviewName, Show = isShow });

                    //if (i == j)
                    //    break;

                }
            }

            myData4ResultImage.Source = listResultImage;
        }

        private void listView1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }
    }


    class MyColorConvert : IValueConverter
        {
            public object Convert(object value, Type typeTarget, object param, CultureInfo culture)
            {
                if (value != null && !value.Equals(""))
                {
                    if (value.Equals("OK"))
                    {
                        return new SolidColorBrush(Colors.Green);
                    }
                    return new SolidColorBrush(Colors.DarkRed);
                }
                else
                {
                    return new SolidColorBrush(Colors.Black);
                }
            }
            public object ConvertBack(object value, Type typeTarget, object param, CultureInfo culture)
            {
                return "";
            }
        }


    class MyShowConvert : IValueConverter
    {
        public object Convert(object value, Type typeTarget, object param, CultureInfo culture)
        {
            if (value != null && value.Equals(true))
            {
                return Visibility.Visible;
            }
            else
            {
                return   Visibility.Collapsed;
            }
        }
        public object ConvertBack(object value, Type typeTarget, object param, CultureInfo culture)
        {
            return "";
        }
    }


    

        public class ResultImage : MyViewModel
    {

        bool show;
        public bool Show { get { return show; } set { show = value;
                OnPropertyChanged(() => this.Show);
            } }
        public string CameraNo { get; set; }
        public int Index { get; set; }
        public string Des { get; set; }
        /// <summary>
        /// 行下标
        /// </summary>
        public int RowIndex { get; set; }
        /// <summary>
        /// 列下标
        /// </summary>
        public int ColIndex { get; set; }


        /// <summary>
        /// 位置-行坐标
        /// </summary>
        public double RowCoordinate { get; set; }

        /// <summary>
        /// 位置-列坐标
        /// </summary>
        public double ColCoordinate { get; set; }
        /// <summary>
        /// 缺陷小图路径
        /// </summary>
        public string ImagePath_NG { get; set; }



        public string ImagePath_OK
        {
            get { return ImagePath_NG.Replace("NG", "OG"); }
        }


        public string ImageName { get; set; }





        string reviewNo = "";


        public double Width { get; set; } = 120;
        public double Height { get; set; } = 120;

public string ReviewNo
{
  get { return reviewNo; }
  set { reviewNo = value; }
}
        /// <summary>
      
        string reviewName = "";

public string ReviewName
{
  get { return reviewName; }
  set { reviewName = value; 
       OnPropertyChanged(() => this.ReviewName);
  }

}
      
        /// <summary>
        /// 复检缺陷排序
        /// </summary>
int reviewSortNo = -1;

public int ReviewSortNo
{
    get { return reviewSortNo; }
    set { reviewSortNo = value; }
}
      


    }
}
